using System;
using System.Collections.Generic;
using UnityEngine;
using Voodoo.Sauce.Common.Utils;
using Voodoo.Sauce.Core;
using Voodoo.Sauce.CrashReport;
using Voodoo.Sauce.Internal.DebugScreen;

namespace Voodoo.Sauce.Debugger
{
	public class CrashReporterDebugScreen : Screen
	{
		private const string CRASH_REPORTER_CHOSEN_MESSAGE = "The crash reporter is set to {0}, restart the application to apply the modifications.";

		private const string CRASH_REPORTER_UNAVAILABILITY_MESSAGE = "The crash reporter is not available in the Unity Editor.";

		private const string CRASH_REPORTER_UPDATE_MESSAGE = "Changing the crash reporter will affect error reporting.";

		[SerializeField]
		private DebugPopup debugPopup;

		private CrashReportCore.CrashReporter _crashReporter;

		private readonly Dictionary<CrashReportCore.CrashReporter, Widget> _crashReportersToggle = new Dictionary<CrashReportCore.CrashReporter, Widget>();

		private void Start()
		{
			_crashReporter = VoodooSauceCore.GetCrashReport().GetCrashReporter();
			foreach (CrashReportCore.CrashReporter value in Enum.GetValues(typeof(CrashReportCore.CrashReporter)))
			{
				if (value == CrashReportCore.CrashReporter.None)
				{
					continue;
				}
				float userPercentage = VoodooSauceCore.GetCrashReport().GetUserPercentage(value);
				if (userPercentage == 0f)
				{
					_crashReportersToggle.Add(value, Label(value.ToString() + " (disabled)"));
					continue;
				}
				_crashReportersToggle.Add(value, Toggle($"{value.ToString()} ({userPercentage}%)", value == _crashReporter, delegate(bool isOn)
				{
					CrashReporterSwitch(isOn, value);
				}));
			}
			Label(PlatformUtils.UNITY_EDITOR ? "The crash reporter is not available in the Unity Editor." : "Changing the crash reporter will affect error reporting.");
		}

		private void CrashReporterSwitch(bool isOn, CrashReportCore.CrashReporter crashReporter)
		{
			DebugToggleButton debugToggleButton = null;
			if (_crashReportersToggle[crashReporter] is DebugToggleButton)
			{
				debugToggleButton = (DebugToggleButton)_crashReportersToggle[crashReporter];
			}
			if (PlatformUtils.UNITY_EDITOR)
			{
				debugToggleButton?.SetValue(isOn: false);
				DisplayPopup("The crash reporter is not available in the Unity Editor.");
			}
			else if (crashReporter == _crashReporter && !isOn)
			{
				debugToggleButton?.SetValue(isOn: true);
			}
			else if (crashReporter != _crashReporter && isOn)
			{
				CrashReportCore.CrashReporter crashReporter2 = _crashReporter;
				_crashReporter = crashReporter;
				if (_crashReportersToggle[crashReporter2] is DebugToggleButton)
				{
					((DebugToggleButton)_crashReportersToggle[crashReporter2]).SetValue(isOn: false);
				}
				debugToggleButton?.SetValue(isOn: true);
				VoodooSauceCore.GetCrashReport().ForceCrashReporter(crashReporter);
				string message = $"The crash reporter is set to {crashReporter.ToString()}, restart the application to apply the modifications.";
				DisplayPopup(message);
			}
		}

		private void DisplayPopup(string message)
		{
			debugPopup.Initialize(message, delegate
			{
				debugPopup.gameObject.SetActive(value: false);
			});
			debugPopup.gameObject.SetActive(value: true);
		}
	}
}
